package csc.fresher.finalgroupfour.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import csc.fresher.finalgroupfour.dao.AccountDAO;
import csc.fresher.finalgroupfour.domain.Account;
import csc.fresher.finalgroupfour.domain.User;

@Service
public class AccountService {
	@Autowired
	private AccountDAO accountDAO;
	
	public void addAccount(Account account) {
		accountDAO.addAccount(account);
	}
	public List<Account> listAccounts() {
		return accountDAO.listAccounts();
	}
	
	//List account by user's role
	public List<Account> listAccounts(User user) {
		String roleName = user.getRole().getRole(); // get user's role name.
		List<Account> accountList = null;
		if (roleName.equals("ROLE_ADMIN")) {
			accountList = listAccounts();
		}
		else if (roleName.equals("ROLE_USER")) {
			accountList = (List<Account>) user.getAccounts();
		}
		return accountList;
	}
	
	public void updateAccount(Account account) {
		accountDAO.updateAccount(account);
	}
	public void deleteAccount(Account account) {
		accountDAO.deleteAccount(account);
	}
	
	public Account findAccount(String accountNumber) {
		return accountDAO.findAccount(accountNumber);
	}
	
}
